package com.gdit.zax.filter;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.RemindDao;
import com.gdit.zax.entitys.Remind;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.thread.DeleteFileThread;
import com.gdit.zax.thread.LoopFileThread;
import com.gdit.zax.thread.LoopRemindThread;
import com.gdit.zax.thread.RemindThread;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.LogUtil;

public class EncodingFilter implements Filter {
	private LoopFileThread lp;
	private DeleteFileThread dft;
	private LoopRemindThread lrth;
	
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain arg2) throws IOException, ServletException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 传递控制到下一个过滤器
		arg2.doFilter(request, response);
	}

	public void init(FilterConfig filterConfig) throws ServletException {
		Constant.WEBPATH = filterConfig.getServletContext().getRealPath("/");
		LogUtil.print("web初始化....");
		lp = new LoopFileThread(Constant.WEBPATH);
		lp.start();
		dft = new DeleteFileThread(new File(Constant.WEBPATH + "upload"),1000*60*15);//15分钟后删除之前上传的缓存文件;
		dft.start();
		lrth = new LoopRemindThread();
		lrth.start();
		
	}

	public void destroy() {
		LogUtil.print("EncodingFilter 销毁");
		try {
			lp.stop();
		} catch (Exception e) {
		}
		try {
			dft.stop();
		} catch (Exception e) {
		}
		try {
			lrth.stop();
		} catch (Exception e) {
		}
	}
}
